package com.panshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panshi.entity.SysRoleFunctionT;
import com.panshi.service.impl.SysRoleFunctionTServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色功能关系表 前端控制器
 * </p>
 *
 * @author xhlMr
 * @since 2020-08-13
 */
@RestController
public class SysRoleFunctionTController {


    @Autowired
    private SysRoleFunctionTServiceImpl sysRoleFunctionTService;


    //角色可能存在多个功能
    //角色表ID查询 角色关系表信息
    @ApiOperation("id查询角色权限关系表")
    @GetMapping("/sys-role-function-t/getRoleFunctionId/{roleId}")
    public List<SysRoleFunctionT> getRoleFunctionID(@PathVariable("roleId") int roleId){
        return sysRoleFunctionTService.list(new QueryWrapper<SysRoleFunctionT>().eq("role_id",roleId));
    }


    @ApiOperation("添加 角色权限关系")
    @PostMapping("/sys-role-function-t/roleFunAdd")
    public Boolean roleFunAdd(@RequestBody SysRoleFunctionT sysRoleFunctionT){
        System.out.println("添加----"+sysRoleFunctionT);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id",sysRoleFunctionT.getRoleId());
        wrapper.eq("function_id",sysRoleFunctionT.getFunctionId());
        SysRoleFunctionT one = sysRoleFunctionTService.getOne(wrapper);
        //没有该权限才添加
        if(one==null){
            return  sysRoleFunctionTService.save(sysRoleFunctionT);
        }
        return false;
    }

    @ApiOperation("删除 Id 角色权限关系")
    @PostMapping("/sys-role-function-t/roleFunDel")
    public Boolean roleFunDel(@RequestBody SysRoleFunctionT sysRoleFunctionT){
        System.out.println("添加----"+sysRoleFunctionT);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id",sysRoleFunctionT.getRoleId());
        wrapper.eq("function_id",sysRoleFunctionT.getFunctionId());
        return sysRoleFunctionTService.remove(wrapper);
    }

}

